import * as services from 'services/wechat';
import { jsApiList } from '../config';
import { Toast } from 'antd-mobile';
import { routerRedux } from 'dva/router';
import wx from 'wx';

export default {
  namespace: 'wechat',

  state: {
    token: {},
    disabled: false,
  },

  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },
  },

  effects: {
    * init({ payload:{ url, hid } }, { call, put }) {
      const token = yield call(services.getTicket, url, hid);
      if(token && token.data){
        wx.config({
          debug: false,
          appId: token.data.appid,
          nonceStr: token.data.noncestr,
          signature: token.data.signature,
          timestamp: token.data.timestamp,
          jsApiList: ['chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'scanQRCode','translateVoice'],
        });
        wx.ready(() => {
          // wx.chooseImage({
          //     count: 1, // 默认9，这里每次只处理一张照片
          //     sizeType: ['original', 'compressed'],   // 可以指定是原图还是压缩图，默认二者都有
          //     sourceType: 'album',        // 可以指定来源是相册还是相机，默认二者都有
          //     success: function (res) {
          //     }
          // });
        });
        wx.error((res) => {
        });
      }
    },

    * chooseImage({ payload: { sourceType, actionType, onSuccess, onError,dispatch, ...rest } }, { call, put }) {
      sourceType = sourceType || ['album', 'camera'];
      wx.chooseImage({//拍照或从手机相册中选图接口
        // 默认9
        count: 1,

        // 可以指定是原图还是压缩图，默认二者都有
        sizeType: ['compressed'],

        // 可以指定来源是相册还是相机，默认二者都有
        sourceType,

        success(res) {
          // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
          const localIds = res.localIds;
            // wx.getLocalImgData 获取本地图片接口
          wx.uploadImage({// 上传图片接口
            // 需要上传的图片的本地ID，由chooseImage接口获得
            localId: localIds[0],

            // 默认为1，显示进度提示
            isShowProgressTips: 1,

            success(res) {
              // 返回图片的服务器端ID: res.serverId;
              const payload = {
                image: res.serverId,
                localId: res.localId,
                ...rest,
              };
                if (onSuccess && typeof onSuccess === 'function') {
                  onSuccess(payload);
                };
            },
            fail(res) {
              Toast.fail('请重新登录',1,()=>{
                if (onError) {
                  dispatch(routerRedux.push('/signin'));
                }
                
              })
            },
          });
        },
      });
    },

    * scanQRCode({ payload: { onSuccess, needResult, ...rest } }, { call, put }) {
      needResult = needResult || 1;
      wx.scanQRCode({
        // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
        needResult,
        // 可以指定扫二维码还是一维码，默认二者都有
        scanType: ['qrCode', 'barCode'],
        success(res) {
          if (typeof onSuccess === 'function') {
            onSuccess(res.resultStr);
          } else {
            throw new TypeError(`wechat/scanQRCode: callback should be a function, but got ${typeof onSuccess}`);
          }
        },
      });
    },

    // * fetchScale({ payload: { idnumber }}, { call, put }){
    //     var nextid = yield call(services.findbyid, idnumber);
    // }
  },

  subscriptions: {
    setup({ dispatch, history }) {
      // return history.listen(({ pathname, query }) => {
      // });
    },
  },
};
